package com.isoftstone.role.service.impl;

import com.isoftstone.platform.entity.PadingRstType;
import com.isoftstone.platform.entity.PagingBean;
import com.isoftstone.role.entity.RoleEntity;
import com.isoftstone.role.entity.RoleMenuInfo;
import com.isoftstone.role.entity.RoleMenuInfoTreeEntity;
import com.isoftstone.role.entity.SearchRoleEntity;
import com.isoftstone.role.repository.RoleDao;
import com.isoftstone.role.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService  {
    //注入规则，变量名一致实例
    @Resource
    RoleDao roleDao;
    
    /** {@inheritDoc} */
    
    @Override
    public PadingRstType<RoleEntity> getRoleList(SearchRoleEntity search, PagingBean pagingBean)
    {
        pagingBean.deal(RoleEntity.class);
        List<RoleEntity> roleBeanList = roleDao.queryRoleByPageList(search, pagingBean);
        //        log.info(JSON.toJSONString(warehouseBeanList));
        Integer total = roleDao.queryRoleTotal(search);
        PadingRstType<RoleEntity> roleBean = new PadingRstType<RoleEntity>();
        roleBean.setPage(pagingBean.getPage());
        roleBean.setTotal(total);
        roleBean.putItems(RoleEntity.class, roleBeanList);
        return roleBean;
    }



    /** {@inheritDoc} */

    @Override
    public void addRole(RoleEntity roleEntity)
    {
        roleDao.addRole(roleEntity);
        // TODO Auto-generated method stub

    }

    @Override
    public void updateRoleDail(RoleEntity roleEntity) {

     roleDao.updateRoleDail(roleEntity);
    }

    @Override
    public String deleteRoleInfo(String roleuuid)
    {
        Integer run = null;//与角色有联系的用户数量
        Integer rdn = null;//与角色有联系的岗位数量
        run = roleDao.queryRoleUserById(roleuuid);
        System.out.println(run);
        if(run>0){
            return "该角色下还有用户，不能删除";
        }
        rdn = roleDao.queryRoleDutyById(roleuuid);
        System.out.println(rdn);
        if(rdn>0){
            return "该角色与岗位有关，不能删除";
        }
        roleDao.cancelPermissions(roleuuid);
        roleDao.deleteRoleInfo(roleuuid);
        return "删除角色成功！";

    }

    @Override
    public List<RoleEntity> queryRoleInfo()
    {
        // TODO Auto-generated method stub
        List<RoleEntity> roleEntityList = roleDao.queryroleInfo();
        return roleEntityList;
    }

    @Override
    public List<RoleEntity> queryRoleListById(String id)
    {
        List<RoleEntity> roleList = roleDao.queryRoleListById(id);
        return roleList;
    }

    @Override
    public List<RoleMenuInfoTreeEntity> queryRoleMenuInfoZtreeList(@RequestParam("roleuuid")String roleuuid) {
        List<RoleMenuInfoTreeEntity> list = roleDao.queryRoleMenuInfoZtreeList();
        List<RoleMenuInfo> roleMenuInfos = roleDao.queryMenuIdByRoleuuid(roleuuid);
        if (roleMenuInfos.size()>0){
            for (RoleMenuInfoTreeEntity entity : list) {
                for (RoleMenuInfo info : roleMenuInfos) {
                    if(info.getMenuId().equals(entity.getId())){
                        entity.setChecked(true);
                    }
                }
            }
        }

        return list;
    }

    @Override
    public void grantRole(String roleuuid,String[] mid) {
        for (String s : mid) {
            int i = Integer.valueOf(s);
            roleDao.grantRole(roleuuid,i);
        }
    }

    @Override
    public void cancelPermissions(String roleuuid) {
        roleDao.cancelPermissions(roleuuid);
    }
}
